<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/3/23 0023
 * Time: 16:13
 */
namespace app\api\controller;
use app\models\Models;
use app\service\Redis;
use app\service\Rsa;

class Register extends Base{
    /**
     * App注册验证昵称
     * @return \think\response\Json
     */
    public function checkNickname(){
        if(request()->isPost()){
            $post = input('param.');
            try{
                // 检查参数
                self::sign(['equipment']);
                // 场景验证
                $validate = $this->validate($post,'User.checkNickname');
                if(true !== $validate){
                    throw new \LogicException($validate,1016);
                }
                $redis = new Redis();
                // 检查昵称是否被锁定
                $val = $redis->get($post['nickname']);
                if($val && $val != $post['equipment']){
                    throw new \LogicException('该昵称已存在',1010);
                }
                // 锁定昵称五分钟
                $bool = $redis->set($post['nickname'],300,$post['equipment']);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult());
        }
    }

    /**
     * App注册验证手机号和密码
     * @return \think\response\Json
     */
    public function checkUser(){
        if(request()->isPost()){
            $post = input('param.');
            try{
                // 检查参数
                self::sign(['equipment','username','password']);
                // 场景验证
                $validate = $this->validate($post,'User.checkUser');
                if(true !== $validate){
                    throw new \LogicException($validate,1016);
                }
            } catch (\LogicException $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult());
        }
    }

    /**
     * App注册
     * @return \think\response\Json
     */
    public function register(){
        if(request()->isPost()){
            $post = input('param.');
            $equipment = $post['equipment'];
            try{
                self::sign(['equipment','username','password','nickname','sex','birth','code','version','platform']);
                // 场景验证
                $validate = $this->validate($post,'User.register');
                if(true !== $validate){
                    throw new \LogicException($validate,1016);
                }
                // 比对验证码
                $redis = new Redis();
                /*$code  = $redis->get($post['username'].'register');
                if(!$code || $post['code'] != $code){
                    throw new \LogicException('验证码错误',1020);
                }*/
                // 图片上传
                if(!$_FILES){
                    throw new \LogicException('上传文件不能为空',1033);
                }
                // 组合注册信息
                unset($post['equipment'],$post['code']);
                $post['portrait'] = upload('portrait')[0];
                $post['password'] = password_hash($post['password'],true);
                $post['create_time'] = time();
                // 开始注册
                $user = new Models('user');
                $uid = $user->_add($post);
                // 用户账户不存在则开户
                $account = new Models('account');
                $account_info = $account->_find(['uid'=>$uid]);
                if(!$account_info){
                    $bool = $account->_add(['uid'=>$uid]);
                    if(!$bool){
                        throw new \LogicException('操作失败',1010);
                    }
                }
                // 生成token
                $token = self::setToken($equipment,$post['username'],$post['password']);
                // 将token存入redis
                $redis->set($token,30*24*3600,$uid);
                // 根据用户id将设备号存入redis,判断异地登录
                $redis->set($uid,30*24*3600,$equipment);
            } catch (\LogicException $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult(['token'=>$token]));
        }
    }
}